﻿<Router AppAssembly="typeof(Program).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="routeData" DefaultLayout="typeof(MainLayout)" />
    </Found>
    <NotFound>
        <LayoutView Layout="typeof(MainLayout)">
            <p>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>

@code {
    protected override async Task OnInitializedAsync()
    {
        // Session ID
        var sessionId = await this.JsRuntime.StorageRead("sessionId");
        if (!string.IsNullOrWhiteSpace(sessionId))
        {
            this.State.SessionId = sessionId;
        }
        else
        {
            await this.JsRuntime.StorageSave("sessionId", this.State.SessionId);
        }

        // User token
        var token = await this.JsRuntime.ReadToken(); // Cannot be blocking call: https://github.com/aspnet/Blazor/issues/1185
        if (!string.IsNullOrEmpty(token))
        {
            this.State.UserToken = token;
            var applicationStartResponse = await this.ApiClient.ApplicationStart();
            if (applicationStartResponse.IsOk)
            {
                this.State.Username = applicationStartResponse.Data.Username;
            }
            else
            {
                Console.WriteLine("Application start error: " + applicationStartResponse.Errors.FirstOrDefault());
            }
        }
    }
}
